package com.tempjob.common.businesss.service;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.tempjob.common.businesss.mongo.service.SysSmsLogService;
import com.tempjob.common.enums.SmsTypeEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author hui
 * @Date 2024/10/9 14:01
 **/
@Slf4j
@Service
public class AliSmsService {

    @Autowired
    private com.aliyun.dysmsapi20170525.Client smsClient;

    @Autowired
    private SysSmsLogService sysSmsLogService;

    public String sendNoticeSms(String phone, SmsTypeEnums smsType, String templateParam) {
        // 初始化请求客户端
        // 构造请求对象，请填入请求参数值
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)//接收短信的手机号码
                .setSignName("蓉锦通")//短信签名名称
                .setTemplateCode(smsType.getCode())//短信模板Code
                .setTemplateParam(templateParam);//短信模板变量对应的实际值
//                .setTemplateParam("{\"name\":\"张三\",\"number\":\"1390000****\"}");//短信模板变量对应的实际值
        try {
            SendSmsResponse sendSmsResponse = smsClient.sendSms(sendSmsRequest);
            log.info("阿里云发送短信：{}，{}", JSON.toJSONString(sendSmsRequest), JSON.toJSONString(sendSmsResponse));
            if (!com.aliyun.teautil.Common.equalString(sendSmsResponse.body.code, "OK")) {
                return null;
            }
            // 响应包含服务端响应的 body 和 headers
            return sendSmsResponse.body.requestId;
        } catch (Exception e) {
            log.error("短信发送错误！", e);
        }
        // 获取响应对象
        return null;
    }


}
